// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: null,
    hasUser: false,
    showPwd: true,
    img: "unshow"
  },
  getInfo: function(e) {
    var that = this
    console.log(e)
    if (e.detail.value.userAccount == "") {
      setTimeout(function() {
        wx.showModal({
          title: '提示',
          content: '请输入你的账号',
          confirmText: "好的",
          confirmColor: "#4fa0e4",
          showCancel: false
        })
      }, 0)
    } 
    // else if (e.detail.value.userAccount.length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(e.detail.value.userAccount)) {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请输入正确的手机号码',
    //     confirmText: "好的",
    //     confirmColor: "#4fa0e4",
    //     showCancel: false
    //   })
    // } 
    else if (e.detail.value.userPwd == "") {
      wx.showModal({
        title: '提示',
        content: '请输入账号密码',
        confirmText: "好的",
        confirmColor: "#4fa0e4",
        showCancel: false
      })
    } else {
      wx.request({
        url: 'https://www.maintenance.ink/student/user/loginPost',
        header: {
          'content-type': 'application/json;charset=utf-8'
        },
        method: "POST",
        data: {
          userAccount: e.detail.value.userAccount,
          userPwd: e.detail.value.userPwd
        },
        success: res => {
          console.log(res)
          var userName = res.data.message.userName
          if (res.data.message.userAccount != e.detail.value.userAccount) {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: "#4fa0e4",
              showCancel: false
            })
          } else if (res.data.message.userPassword != e.detail.value.userPwd) {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: "#4fa0e4",
              showCancel: false
            })
          } else {
            wx.showToast({
              title: '登陆成功',
              duration: 1500,
              success: res => {
                wx.setStorageSync("userName", userName)
                that.setData({
                  hasUser: true,
                  userName: userName
                })
              }
            })
          }
        }
      })
    }
  },
  nouser: function() { //注销
    var that = this
    setTimeout(function() {
      setTimeout(function() {
        wx.hideLoading()
        wx.clearStorageSync("userName")
        that.setData({
          hasUser: false
        })
      }, 1000)
      wx.showLoading({
        title: '正在注销'
      })
    }, 500)
  },
  register: function() { //注册
    wx.navigateTo({
      url: '/pages/register/register'
    })
  },
  forgetPwd: function() { //忘记密码
    wx.navigateTo({
      url: '/pages/forgetPwd/forgetPwd'
    })
  },
  showPwd() {
    this.setData({
      showPwd: this.data.showPwd ? false : true,
      img: this.data.showPwd ? "show" : "unshow"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var userName = wx.getStorageSync("userName")
    console.log(userName)
    if (userName) {
      that.setData({
        hasUser: true,
        userName: userName
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#4fa0e4',
    })
    var that = this
    var userName = wx.getStorageSync("userName")
    console.log(userName)
    if (userName) {
      that.setData({
        hasUser: true,
        userName: userName
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})